﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;
using System.Collections.Generic;


/// <summary>
/// Summary description for ErrorLogDAL
/// </summary>
public class ErrorLogDAL
{
    SqlConnection connection;
    SqlCommand cmd;

	public ErrorLogDAL()
	{
        try
        {
            /*initilaize db variables*/
            connection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["loanConnectionString"].ConnectionString);
            cmd = new SqlCommand("", connection);
        }
        catch
        {
            throw new Exception("Unable to connect to database");
        }
	}

    public int InsertErrors_old(DataTable errors)
    {
        if (errors != null)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "InsertErrors";
            cmd.Parameters.AddWithValue("@errors", errors);
            SqlParameter param = new SqlParameter("@log_id", SqlDbType.Int);
            param.Direction= ParameterDirection.ReturnValue;
            cmd.Parameters.Add(param);

            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                return (int)cmd.Parameters["@log_id"].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd.Connection.State != ConnectionState.Closed)
                    cmd.Connection.Close();
            }
        }
        else
        {
            return 0;
        }
    }

    public int InsertErrors(string errors)
    {
        if (errors != null)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "InsertErrors";
            cmd.Parameters.Add("@errors", SqlDbType.Xml);
            cmd.Parameters["@errors"].Value = errors;
            SqlParameter param = new SqlParameter("@log_id", SqlDbType.Int);
            param.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(param);

            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                return (int)cmd.Parameters["@log_id"].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cmd.Connection.State != ConnectionState.Closed)
                    cmd.Connection.Close();
            }
        }
        else
        {
            return 0;
        }
    }

    public int InsertException(Error exception)
    {
        if (exception != null)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "InsertException";
            cmd.Parameters.AddWithValue("@error_type", exception.Type);
            cmd.Parameters.AddWithValue("error_code", exception.Code);
            cmd.Parameters.AddWithValue("error_msg", exception.Message);
            cmd.Parameters.AddWithValue("stack_trace", exception.StackTrace);
            cmd.Parameters.AddWithValue("error_date", exception.Date);
            cmd.Parameters.AddWithValue("additional_details", exception.AdditionalDetails);
            SqlParameter param = new SqlParameter("@log_id", SqlDbType.Int);
            param.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(param);

            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                return (int)cmd.Parameters["@log_id"].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (cmd.Connection.State != ConnectionState.Closed)
                        cmd.Connection.Close();
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
        }
        else
        {
            return 0;
        }
    }
    
}